package app.projeto.dao;

import java.sql.ResultSet;
import java.sql.Statement;

import app.projeto.entity.Log;
import app.projeto.entity.Usuario;
import app.projeto.util.Factory;
import app.projeto.util.Query;

public class DaoLog extends Dao{

	/**
	 * Busca pelos registros do objeto PROJETO
	 * 
	 * @param obj
	 * @return
	 * @throws Exception
	 */
	public Object[] findByProjeto(Object obj) throws Exception {
		Log log = (Log) obj;
		try {
			String sql = "select " + Query.getFieldNames(log, true) + " from " + Query.getTableName(log) +
							" where idprojeto = " + log.getProjeto().getIdProjeto() + " and idatividade is null and idmilestone is null" ;
			System.out.println(sql);
			Statement stmt = this.getConnection().createStatement();
			ResultSet rst = stmt.executeQuery(sql);
			return Factory.createByResultSet(rst, obj.getClass());
		} catch (Exception e) {
			this.rollback();
			throw e;
		}
	}
	
	/**
	 *  Busca pelos registros do objeto PROJETO e DATAS	 * 
	 * 
	 * @param obj
	 * @param inicio
	 * @param fim
	 * @return
	 * @throws Exception
	 */
	public Object[] findByProjetoEData(Object obj, String inicio, String fim) throws Exception {
		Log log = (Log) obj;
		
		try {
			String sql = "select " + Query.getFieldNames(log, true) + " from " + Query.getTableName(log) +
							" where idprojeto = " + log.getProjeto().getIdProjeto() + " and idatividade is null and idmilestone is null" +
							" and datahora between '" + inicio + "' and '" + fim + "'";
			System.out.println(sql);
			Statement stmt = this.getConnection().createStatement();
			ResultSet rst = stmt.executeQuery(sql);
			return Factory.createByResultSet(rst, obj.getClass());
		} catch (Exception e) {
			this.rollback();
			throw e;
		}
	}
	
	/**
	 *  Busca pelos registros do objeto PROJETO e DATAS e USUARIO
	 * 
	 * @param obj
	 * @param inicio
	 * @param fim
	 * @param usuario
	 * @return
	 * @throws Exception
	 */
	public Object[] findByProjetoEDataEUsuario(Object obj, String inicio, String fim, Usuario usuario) throws Exception {
		Log log = (Log) obj;
		try {
			String sql = "select " + Query.getFieldNames(log, true) + " from " + Query.getTableName(log) +
							" where idprojeto = " + log.getProjeto().getIdProjeto() + " and idatividade is null and idmilestone is null" +
							" and datahora between '" + inicio + "' and '" + fim + "' " +
							" and idusuario = " + usuario.getIdUsuario();
			System.out.println(sql);
			Statement stmt = this.getConnection().createStatement();
			ResultSet rst = stmt.executeQuery(sql);
			return Factory.createByResultSet(rst, obj.getClass());
		} catch (Exception e) {
			this.rollback();
			throw e;
		}
	}
	
	/**
	 *  Busca pelos registros do objeto PROJETO e USUARIO
	 * 
	 * @param obj
	 * @param usuario
	 * @return
	 * @throws Exception
	 */
	public Object[] findByProjetoEUsuario(Object obj, Usuario usuario) throws Exception {
		Log log = (Log) obj;
		try {
			String sql = "select " + Query.getFieldNames(log, true) + " from " + Query.getTableName(log) +
							" where idprojeto = " + log.getProjeto().getIdProjeto() + " and idatividade is null and idmilestone is null" +
							" and idusuario = " + usuario.getIdUsuario();
			System.out.println(sql);
			Statement stmt = this.getConnection().createStatement();
			ResultSet rst = stmt.executeQuery(sql);
			return Factory.createByResultSet(rst, obj.getClass());
		} catch (Exception e) {
			this.rollback();
			throw e;
		}
	}
	
	/**
	 *  Busca pelos registros do objeto ATIVIDADE 
	 * 	
	 * @param obj
	 * @return
	 * @throws Exception
	 */
	public Object[] findByAtividade(Object obj) throws Exception {
		Log log = (Log) obj;
		try {
			String sql = "select " + Query.getFieldNames(log, true) + " from " + Query.getTableName(log) +
							" where idprojeto = " + log.getProjeto().getIdProjeto() + " and idatividade is not null and idmilestone is null" ;
			System.out.println(sql);
			Statement stmt = this.getConnection().createStatement();
			ResultSet rst = stmt.executeQuery(sql);
			return Factory.createByResultSet(rst, obj.getClass());
		} catch (Exception e) {
			this.rollback();
			throw e;
		}
	}
	
	/**
	 *  Busca pelos registros do objeto ATIVIDADE e DATAS
	 * 
	 * @param obj
	 * @param inicio
	 * @param fim
	 * @return
	 * @throws Exception
	 */
	public Object[] findByAtividadeEData(Object obj, String inicio, String fim) throws Exception {
		Log log = (Log) obj;
		
		try {
			String sql = "select " + Query.getFieldNames(log, true) + " from " + Query.getTableName(log) +
							" where idprojeto = " + log.getProjeto().getIdProjeto() + " and idatividade is not null and idmilestone is null" +
							" and datahora between '" + inicio + "' and '" + fim + "'";
			System.out.println(sql);
			Statement stmt = this.getConnection().createStatement();
			ResultSet rst = stmt.executeQuery(sql);
			return Factory.createByResultSet(rst, obj.getClass());
		} catch (Exception e) {
			this.rollback();
			throw e;
		}
	}
	
	/**
	 *  Busca pelos registros do objeto ATIVIDADE e DATAS e USUARIO
	 * 
	 * @param obj
	 * @param inicio
	 * @param fim
	 * @param usuario
	 * @return
	 * @throws Exception
	 */
	public Object[] findByAtividadeEDataEUsuario(Object obj, String inicio, String fim, Usuario usuario) throws Exception {
		Log log = (Log) obj;
		try {
			String sql = "select " + Query.getFieldNames(log, true) + " from " + Query.getTableName(log) +
							" where idprojeto = " + log.getProjeto().getIdProjeto() + " and idatividade is not null and idmilestone is null" +
							" and datahora between '" + inicio + "' and '" + fim + "' " +
							" and idusuario = " + usuario.getIdUsuario();
			System.out.println(sql);
			Statement stmt = this.getConnection().createStatement();
			ResultSet rst = stmt.executeQuery(sql);
			return Factory.createByResultSet(rst, obj.getClass());
		} catch (Exception e) {
			this.rollback();
			throw e;
		}
	}
	
	/**
	 *  Busca pelos registros do objeto ATIVIDADE e USUARIO
	 * 
	 * @param obj
	 * @param usuario
	 * @return
	 * @throws Exception
	 */
	public Object[] findByAtividadeEUsuario(Object obj, Usuario usuario) throws Exception {
		Log log = (Log) obj;
		try {
			String sql = "select " + Query.getFieldNames(log, true) + " from " + Query.getTableName(log) +
							" where idprojeto = " + log.getProjeto().getIdProjeto() + " and idatividade is not null and idmilestone is null" +
							" and idusuario = " + usuario.getIdUsuario();
			System.out.println(sql);
			Statement stmt = this.getConnection().createStatement();
			ResultSet rst = stmt.executeQuery(sql);
			return Factory.createByResultSet(rst, obj.getClass());
		} catch (Exception e) {
			this.rollback();
			throw e;
		}
	}
	
	/**
	 *  Busca pelos registros do objeto MILESTONE
	 * 
	 * @param obj
	 * @return
	 * @throws Exception
	 */
	public Object[] findByMilestone(Object obj) throws Exception {
		Log log = (Log) obj;
		try {
			String sql = "select " + Query.getFieldNames(log, true) + " from " + Query.getTableName(log) +
							" where idprojeto = " + log.getProjeto().getIdProjeto() + " and idatividade is null and idmilestone is not null" ;
			System.out.println(sql);
			Statement stmt = this.getConnection().createStatement();
			ResultSet rst = stmt.executeQuery(sql);
			return Factory.createByResultSet(rst, obj.getClass());
		} catch (Exception e) {
			this.rollback();
			throw e;
		}
	}
	
	/**
	 * Busca pelos registros do objeto MILESTONE e DATAS
	 * 
	 * @param obj
	 * @param inicio
	 * @param fim
	 * @return
	 * @throws Exception
	 */
	public Object[] findByMilestoneEData(Object obj, String inicio, String fim) throws Exception {
		Log log = (Log) obj;
		
		try {
			String sql = "select " + Query.getFieldNames(log, true) + " from " + Query.getTableName(log) +
							" where idprojeto = " + log.getProjeto().getIdProjeto() + " and idatividade is null and idmilestone is not null" +
							" and datahora between '" + inicio + "' and '" + fim + "'";
			System.out.println(sql);
			Statement stmt = this.getConnection().createStatement();
			ResultSet rst = stmt.executeQuery(sql);
			return Factory.createByResultSet(rst, obj.getClass());
		} catch (Exception e) {
			this.rollback();
			throw e;
		}
	}
	
	/**
	 * Busca pelos registros do objeto MILESTONE e DATAS e USUARIO
	 * 
	 * @param obj
	 * @param inicio
	 * @param fim
	 * @param usuario
	 * @return
	 * @throws Exception
	 */
	public Object[] findByMilestoneEDataEUsuario(Object obj, String inicio, String fim, Usuario usuario) throws Exception {
		Log log = (Log) obj;
		try {
			String sql = "select " + Query.getFieldNames(log, true) + " from " + Query.getTableName(log) +
							" where idprojeto = " + log.getProjeto().getIdProjeto() + " and idatividade is null and idmilestone not is null" +
							" and datahora between '" + inicio + "' and '" + fim + "' " +
							" and idusuario = " + usuario.getIdUsuario();
			System.out.println(sql);
			Statement stmt = this.getConnection().createStatement();
			ResultSet rst = stmt.executeQuery(sql);
			return Factory.createByResultSet(rst, obj.getClass());
		} catch (Exception e) {
			this.rollback();
			throw e;
		}
	}
	
	/**
	 * Busca pelos registros do objeto MILESTONE e USUARIO
	 * 
	 * @param obj
	 * @param usuario
	 * @return
	 * @throws Exception
	 */
	public Object[] findByMilestoneEUsuario(Object obj, Usuario usuario) throws Exception {
		Log log = (Log) obj;
		try {
			String sql = "select " + Query.getFieldNames(log, true) + " from " + Query.getTableName(log) +
							" where idprojeto = " + log.getProjeto().getIdProjeto() + " and idatividade is null and idmilestone is not null" +
							" and idusuario = " + usuario.getIdUsuario();
			System.out.println(sql);
			Statement stmt = this.getConnection().createStatement();
			ResultSet rst = stmt.executeQuery(sql);
			return Factory.createByResultSet(rst, obj.getClass());
		} catch (Exception e) {
			this.rollback();
			throw e;
		}
	}
	
	/**
	 * Busca pelos registros por DATA e USUARIO
	 * 
	 * @param obj
	 * @param inicio
	 * @param fim
	 * @param usuario
	 * @return
	 * @throws Exception
	 */
	public Object[] findByDataEUsuario(Object obj, String inicio, String fim, Usuario usuario) throws Exception {
		Log log = (Log) obj;
		try {
			String sql = "select " + Query.getFieldNames(log, true) + " from " + Query.getTableName(log) +
							" where idprojeto = " + log.getProjeto().getIdProjeto() + 
							" and datahora between '" + inicio + "' and '" + fim + "' " +
							" and idusuario = " + usuario.getIdUsuario();
			System.out.println(sql);
			Statement stmt = this.getConnection().createStatement();
			ResultSet rst = stmt.executeQuery(sql);
			return Factory.createByResultSet(rst, obj.getClass());
		} catch (Exception e) {
			this.rollback();
			throw e;
		}
	}
	
	/**
	 * Busca pelos registros por DATA
	 * 
	 * @param obj
	 * @param inicio
	 * @param fim
	 * @return
	 * @throws Exception
	 */
	public Object[] findByData(Object obj, String inicio, String fim) throws Exception {
		Log log = (Log) obj;
		try {
			String sql = "select " + Query.getFieldNames(log, true) + " from " + Query.getTableName(log) +
							" where idprojeto = " + log.getProjeto().getIdProjeto() + 
							" and datahora between '" + inicio + "' and '" + fim + "' ";
			System.out.println(sql);
			Statement stmt = this.getConnection().createStatement();
			ResultSet rst = stmt.executeQuery(sql);
			return Factory.createByResultSet(rst, obj.getClass());
		} catch (Exception e) {
			this.rollback();
			throw e;
		}
	}
	
	/**
	 * Busca pelos registros por USUARIO
	 * 
	 * @param obj
	 * @param usuario
	 * @return
	 * @throws Exception
	 */
	public Object[] findByUsuario(Object obj, Usuario usuario) throws Exception {
		Log log = (Log) obj;
		try {
			String sql = "select " + Query.getFieldNames(log, true) + " from " + Query.getTableName(log) +
							" where idprojeto = " + log.getProjeto().getIdProjeto() +
							" and idusuario = " + usuario.getIdUsuario();
			System.out.println(sql);
			Statement stmt = this.getConnection().createStatement();
			ResultSet rst = stmt.executeQuery(sql);
			return Factory.createByResultSet(rst, obj.getClass());
		} catch (Exception e) {
			this.rollback();
			throw e;
		}
	}
	
	/**
	 * Busca por todos os registros em ordem decrescente
	 */
	@Override
	public Object[] find(Object obj) throws Exception {
		try {
			String sql = Query.getSQLSelect(obj) + " desc";
			System.out.println(sql);
			Statement stmt = this.getConnection().createStatement();
			ResultSet rst = stmt.executeQuery(sql);
			return Factory.createByResultSet(rst, obj.getClass());
		} catch (Exception e) {
			this.rollback();
			throw e;
		}
	}
	
	public Object[] findByUsuarioOnly(Usuario usuario) throws Exception {
		Log log = new Log();
		try {
			String sql = "select " + Query.getFieldNames(log, true) + " from " + Query.getTableName(log) +
						" where idusuario = " + usuario.getIdUsuario();
			System.out.println(sql);
			Statement stmt = this.getConnection().createStatement();
			ResultSet rst = stmt.executeQuery(sql);
			return Factory.createByResultSet(rst, log.getClass());
		} catch (Exception e) {
			this.rollback();
			throw e;
		}
	}
}
